<?php
require_once dirname(__FILE__) . '/../Controller.php';
require_once dirname(__FILE__) . '/qdsmtp/qdsmtp.php';
require_once dirname(__FILE__) . '/qdmail/qdmail.php';

class Models_Mail_Controller extends Models_Controller
{
	const KEY_SMTP_HOST = "HOST";
	const KEY_SMTP_PORT = "PORT";
	const KEY_SMTP_PROTOCOL = "PROTOCOL";
	const KEY_MAIL_FROM = "FROM";
	const KEY_MAIL_TO = "TO";
	const KEY_USER = "USER";
	const KEY_PASS = "PASS";
	const KEY_POP_USER = "POP_USER";
	const KEY_POP_PASS = "POP_PASS";
	const KEY_POP_HOST = "POP_HOST";

	const VALUE_SMTP_PROTOCOL_SMTP = 'SMTP';
	const VALUE_SMTP_PROTOCOL_POP_BEFORE = 'POP_BEFORE';
	const VALUE_SMTP_PROTOCOL_SMTP_AUTH = "SMTP_AUTH";

	protected $_param;
	protected $_to;

	/**
	 * コンストラクタ
	 *
	 * @param unknown_type $SMTP_HOST	SMTPが設置されているホスト名
	 * @param unknown_type $MAIL_FROM	メールヘッダに設定するFROMアドレス
	 * @param unknown_type $SMTP_PORT	SMTPポート
	 * @param unknown_type $KEY_SMTP_PROTOCOL	クラス定数_PRTOCOL_から選択
	 * @param unknown_type $USER 		_PROTOCOL_がSMTP以外のとき設定する
	 * @param unknown_type $PASS		_PROTOCOL_がSMTP以外のとき設定する
	 * @param unknown_type $POP_HOST	_PROTOCOL_がPOP_BEFOREのとき設定する
	 */
	public function setParam($SMTP_HOST, $MAIL_FROM, $SMTP_PORT='25', $SMTP_PROTOCOL=self::VALUE_SMTP_PROTOCOL_SMTP, $USER='', $PASS='',$POP_HOST='')
	{
		switch ($SMTP_PROTOCOL) {
			case self::VALUE_SMTP_PROTOCOL_SMTP:
				$this->_param = array(
				    'host'=>$SMTP_HOST,
				    'port'=> $SMTP_PORT ,
				    'from'=> $MAIL_FROM,
				    'protocol'=> $SMTP_PROTOCOL,
				);
				break;
			case self::VALUE_SMTP_PROTOCOL_POP_BEFORE:
				$this->_param = array(
				    'host'=>$SMTP_HOST,
				    'port'=> $SMTP_PORT ,
				    'from'=> $MAIL_FROM,
				    'protocol'=> $SMTP_PROTOCOL,
					'pop_host'=> $POP_HOST,
					'pop_user'=> $USER,
					'pop_pass'=> $PASS,
				);
				break;
			case self::VALUE_SMTP_PROTOCOL_SMTP_AUTH:
				$this->_param = array(
				    'host'=>$SMTP_HOST,
				    'port'=> $SMTP_PORT ,
				    'from'=> $MAIL_FROM,
				    'protocol'=> $SMTP_PROTOCOL,
					'user'=> $USER,
					'pass'=> $PASS,
				);
				break;
		}
	}

	public function setTo( $sTo )
	{
		$this->_to = $sTo;
	}

	public function getPass()
	{
		$sResult = '';
		if ( isset( $this->_param[strtolower(self::KEY_SMTP_PROTOCOL)] ) ) {
			if ( $this->_param[strtolower(self::KEY_SMTP_PROTOCOL)] == self::VALUE_SMTP_PROTOCOL_POP_BEFORE ) {
				if ( isset($this->_param[strtolower(self::KEY_POP_PASS)] ) ) {
					$sResult = $this->_param[strtolower(self::KEY_POP_PASS)];
				}
			} else if ($this->_param[strtolower(self::KEY_SMTP_PROTOCOL)] == self::VALUE_SMTP_PROTOCOL_SMTP_AUTH ) {
				if ( isset($this->_param[strtolower(self::KEY_PASS)]) ) {
					$sResult = $this->_param[strtolower(self::KEY_PASS)];
				}
			}
		}
	}

	public function getUser()
	{
		$sResult = '';
		if ( isset( $this->_param[strtolower(self::KEY_SMTP_PROTOCOL)] ) ) {
			if ( $this->_param[strtolower(self::KEY_SMTP_PROTOCOL)] == self::VALUE_SMTP_PROTOCOL_POP_BEFORE ) {
				if ( isset($this->_param[strtolower(self::KEY_POP_USER)] ) ) {
					$sResult = $this->_param[strtolower(self::KEY_POP_USER)];
				}
			} else if ($this->_param[strtolower(self::KEY_SMTP_PROTOCOL)] == self::VALUE_SMTP_PROTOCOL_SMTP_AUTH ) {
				if ( isset($this->_param[strtolower(self::KEY_USER)]) ) {
					$sResult = $this->_param[strtolower(self::KEY_USER)];
				}
			}
		}
	}

	public function getPopHost()
	{
		$sResult = '';
		if ( isset($this->_param[strtolower(self::KEY_POP_HOST)] ) ) {
			$sResult = $this->_param[strtolower(self::KEY_POP_HOST)];
		}
		return $sResult;
	}

	public function getProtocol()
	{
		$sResult = '';
		if ( isset($this->_param[strtolower(self::KEY_SMTP_PROTOCOL)] ) ) {
			$sResult = $this->_param[strtolower(self::KEY_SMTP_PROTOCOL)];
		}
		return $sResult;
	}

	public function getFrom()
	{
		$sResult = '';
		if ( isset($this->_param[strtolower(self::KEY_MAIL_FROM)] ) ) {
			$sResult = $this->_param[strtolower(self::KEY_MAIL_FROM)];
		}
		return $sResult;
	}

	public function getHost()
	{
		$sResult = '';
		if ( isset($this->_param[strtolower(self::KEY_SMTP_HOST)] ) ) {
			$sResult = $this->_param[strtolower(self::KEY_SMTP_HOST)];
		}
		return $sResult;
	}

	public function getPort()
	{
		$sResult = '';
		if ( isset($this->_param[strtolower(self::KEY_SMTP_PORT)] ) ) {
			$sResult = $this->_param[strtolower(self::KEY_SMTP_PORT)];
		}
		return $sResult;
	}

	public function getTo()
	{
		return $this->_to;
	}

	public function send($sSubject,$sBody, $sTo='')
	{
		if ( $this->_to == '' ) {
			return TRUE;
		}

		$objSmtp = new Qdmail();

		$objSmtp->errorDisplay(FALSE);
		$objSmtp->smtpObject()->error_display = false;
		$objSmtp->smtpLoglevelLink(true);
		$objSmtp->logPath(dirname(__FILE__).'/Logs');
		$objSmtp->logFilename("mail.log");
		$objSmtp->errorlogPath(dirname(__FILE__).'/Logs');
		$objSmtp->errorlogFilename("error_mail.log");

		$objSmtp->smtp(true);
		$objSmtp->smtpServer($this->_param);

		$objSmtp->to($this->_to);
		if ( $sTo != '') {
			$objSmtp->cc($sTo);
		}

		$objSmtp->subject($sSubject);
		$objSmtp->from($this->getFrom());
		$objSmtp->text($sBody);
		$bResult = $objSmtp->send();

		if ( $bResult == FALSE ) {
			return FALSE;
		}

/*		if ( $sTo != '' ) {
			$aryToList = explode(',', $sTo);
			foreach ( $aryToList as $sToAddress ) {
				if ( $sToAddress == $this->_to ) {
					continue;
				}
				$objSmtp->to($sToAddress);
				$bResult = $objSmtp->send();
			}
		}
*/
		return $bResult;
	}

	public function saveParamToXml($src='')
	{
		if ($src=='') {
			$src = dirname(__FILE__) . '/databox/data.xml';
		}

		$this->_SourceName = $src;
		$this->loadFromXml();

		$this->_Data = array();
		foreach ( $this->_param as $key=>$value) {
			$this->_Data[strtoupper($key)] = $value;
		}
		$this->_Data[self::KEY_MAIL_TO] = $this->_to;

		$this->saveToXml();
	}

	public function loadParamFromXml($src='')
	{
		if ($src=='') {
			$src = dirname(__FILE__) . '/databox/data.xml';
		}

		$this->_SourceName = $src;
		$this->loadFromXml();

		if ( $this->_Data === FALSE ) {
			return FALSE;
		}

		$this->_param = array();
		foreach ( $this->_Data as $key=>$value ) {
			$this->_param[strtolower($key)] = $value;
		}

		$this->_to = $this->_Data[self::KEY_MAIL_TO];

		return TRUE;
	}
}